package com.zyp.test.test;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Picture;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.RadialGradient;
import android.graphics.Rect;
import android.graphics.Shader;
import android.graphics.Xfermode;
import android.support.annotation.Nullable;
import android.util.AttributeSet;

/**
 * @author zyp
 *         2017/12/28
 */

public class ShaderTestView extends android.support.v7.widget.AppCompatImageView {

    private Paint mPaint;
    private Shader mShader;
    private RadialGradient mShader1;
    private Xfermode mXfermode;
    private Paint mPaint1;
    private Bitmap mBitmap;
    private Rect mRect;
    private Bitmap mBitmap1;
    private Bitmap mCroppedBitmap;
    private Bitmap mClipBitmap;

    public ShaderTestView(Context context) {
        super(context);
        init();
    }

    public ShaderTestView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public ShaderTestView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        setLayerType(LAYER_TYPE_SOFTWARE, null);
        mPaint = new Paint();
        mPaint.setColor(Color.RED);
        //Shader shader = new LinearGradient(0, 0, 200, getHeight(), Color.YELLOW, Color.BLUE, Shader.TileMode.MIRROR);
        mXfermode = new PorterDuffXfermode(PorterDuff.Mode.SRC_IN);
        mPaint1 = new Paint();
        //mPaint1.setColor(Color.WHITE);
        //mPaint1.setFilterBitmap(true);
       // mPaint1.setXfermode(mXfermode);
        mBitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.icon);
        
        
        mCroppedBitmap = NewCircleImageView.getCroppedBitmap(mBitmap, 100);
        
    }
    @Override
    protected void onDraw(Canvas canvas) {
        //super.onDraw(canvas);
        //canvas.drawRect(0, 0, 500, 500, mPaint);
        /*if(mBitmap1 == null){
            mBitmap1 = Bitmap.createBitmap(getWidth(),getHeight(), Bitmap.Config.ARGB_8888);
            canvas.setBitmap(mBitmap1);
        }*/
        /*canvas.drawARGB(0, 0, 0, 0);
        canvas.drawCircle(500, 500, 250, mPaint1);
        
        mPaint1.setXfermode(mXfermode);
        canvas.drawBitmap(mBitmap,mRect,mRect,mPaint1);
        mPaint1.setXfermode(null);*/
        
       canvas.drawBitmap(mClipBitmap,mRect,mRect,mPaint1);
        //RectF rectF = new RectF(mRect);
        
        /*canvas.drawColor(Color.YELLOW);
        canvas.saveLayer(0,0,1000,1000,mPaint,Canvas.ALL_SAVE_FLAG);
        canvas.drawCircle(400,400,200,mPaint);
        mPaint.setColor(Color.BLUE);
        mPaint.setXfermode(mXfermode);
        canvas.drawRect(0,0,400,400,mPaint);
        mPaint.setXfermode(null);
        canvas.restore();*/
       // canvas.drawPicture(drawPictrue());
        
    }
    private Picture drawPictrue(){
        
        Picture picture = new Picture();
        Canvas canvas = picture.beginRecording(mRect.width(), mRect.height());
        canvas.drawColor(Color.YELLOW);
        canvas.saveLayer(0,0,1000,1000,mPaint,Canvas.ALL_SAVE_FLAG);
        canvas.drawCircle(400,400,200,mPaint);
        mPaint.setColor(Color.BLUE);
        mPaint.setXfermode(mXfermode);
        canvas.drawRect(0,0,400,400,mPaint);
        mPaint.setXfermode(null);
        canvas.restore();
        picture.endRecording();
       // canvas.drawColor(Color.RED);
        return picture;
    }
    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        mRect = new Rect(0,0,getWidth(),getHeight());
        mBitmap = Bitmap.createScaledBitmap(mBitmap,400,400,true);
        mClipBitmap = clipBitmap(mBitmap);
    }

    private Bitmap clipBitmap(Bitmap bitmap){
        Paint paint = new Paint();
        paint.setColor(Color.RED);
        Bitmap output = Bitmap.createBitmap(getWidth(),getHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(output);
        /*canvas.drawCircle(getWidth()/2,getHeight()/2,getWidth()/2,paint);
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_OVER));
        Rect rectF = new Rect(0,0,bitmap.getWidth(),bitmap.getHeight());
        canvas.drawBitmap(bitmap,rectF,rectF,paint);*/
        
        canvas.drawColor(Color.YELLOW);
        canvas.saveLayer(0,0,1000,1000,mPaint,Canvas.ALL_SAVE_FLAG);
        canvas.drawCircle(400,400,200,paint);
        paint.setColor(Color.BLUE);
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
        Rect rectF = new Rect(0,0,bitmap.getWidth(),bitmap.getHeight());
        canvas.drawBitmap(bitmap,rectF,rectF,paint);
        paint.setXfermode(null);
        canvas.restore();
        return output;
    }
}
